﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Administrator_AdminControl_EstoreInfo : System.Web.UI.UserControl
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadInfo();
            LoadPackage();
    }

    private void LoadInfo()
    {
        try
        {
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                var query = (from o in dc.EStores where o.EStore_StatusId == 2
                             select o).ToList();
                if (query.Count > 0)
                {
                    PagedDataSource page = new PagedDataSource();
                    page.AllowCustomPaging = true;
                    page.AllowPaging = true;
                    page.DataSource = query;
                    rptInfo.DataSource = page;
                    rptInfo.DataBind();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void LoadPackage()
    {
        try
        {
            using (Up1VNDataContext db = new Up1VNDataContext())
            {
                var query = (from o in db.PointBackages select o);
                ddrPackage.DataSource = query;
                ddrPackage.DataTextField = "PointBackage_Name";
                ddrPackage.DataValueField = "PointBackage_Id";
                ddrPackage.DataBind();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    
    protected void rptInfo_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "delete")
            {
                using (Up1VNDataContext dc = new Up1VNDataContext())
                {
                    var query = from o in dc.EStores where o.EStore_Id == int.Parse(e.CommandArgument.ToString()) select o;
                    dc.EStores.DeleteAllOnSubmit(query);
                    dc.SubmitChanges();
                    LoadInfo();
                }

            }
            if (e.CommandName == "edit")
            {
                using (Up1VNDataContext dc = new Up1VNDataContext())
                {
                    btnUpdate.Visible = true;
                    pnlDetail.Visible = true;
                    btnSave.Visible = false;
                    var query = (from o in dc.EStores where o.EStore_Id == int.Parse(e.CommandArgument.ToString()) select o).Single();                   
                    lblName.Text = query.EStore_Name;
                    txtName.Text = query.EStore_Name;
                    txtTax.Text = query.EStore_CodeTax;
                    txtRepresent.Text = query.EStore_Representative;
                    txtPhone.Text = query.Estore_HomePhone;
                    txtMobil.Text = query.EStore_MobilePhone;
                    txtSky.Text = query.EStore_NickSkype;
                    txtYahoo.Text = query.EStore_NickYahoo;
                    txtAddress.Text = query.EStore_Address;
                    txtDes.Text = query.EStore_BriefIntroduction;
                    txtWeb.Text = query.EStore_Website;
                    txtFax.Text = query.EStore_Fax;
                    lblEstoreID.Text = query.EStore_Id.ToString();

                    var queryCateJob = (from o in dc.GeneralCategories
                                        where o.GeneralCategory_Type == GeneralCategoryType.CategoryJob
                                  select o);
                    drdJob.DataSource = queryCateJob;
                    drdJob.DataTextField = "GeneralCategory_Name";
                    drdJob.DataValueField = "GeneralCategory_Id";
                    drdJob.DataBind();
                    if (query.EStore_CategoryId != null)
                        drdJob.SelectedValue = query.EStore_CategoryId.ToString();

                    var queryCatCity = (from o in dc.GeneralCategories
                                        where o.GeneralCategory_Type == GeneralCategoryType.CityProvince
                                        select o);
                    drdCity.DataSource = queryCatCity;
                    drdCity.DataTextField = "GeneralCategory_Name";
                    drdCity.DataValueField = "GeneralCategory_Id";
                    drdCity.DataBind();
                    if(query.EStore_ProvinceId != null)
                        drdCity.SelectedValue = query.EStore_ProvinceId.ToString();

                    

                    var queryUser = (from o in dc.Users select o);
                    drdUser.DataSource = queryUser;
                    drdUser.DataTextField = "Users_Name";
                    drdUser.DataValueField = "Users_Id";
                    drdUser.SelectedValue = query.Users_Id.ToString();
                    drdUser.DataBind();
                }
            }

            

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            int id = int.Parse(lblEstoreID.Text);
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                EStore E = dc.EStores.Single(s => s.EStore_Id == id);
                E.EStore_Name = txtName.Text;
                E.EStore_CodeTax = txtTax.Text;
                E.EStore_Representative = txtRepresent.Text;
                E.Estore_HomePhone = txtPhone.Text;
                E.EStore_MobilePhone = txtMobil.Text;
                E.EStore_NickSkype = txtSky.Text;
                E.EStore_NickYahoo = txtYahoo.Text;
                E.EStore_Address = txtAddress.Text;
                E.EStore_BriefIntroduction = txtDes.Text;
                E.EStore_Website = txtWeb.Text;
                E.EStore_Fax = txtFax.Text;
                E.EStore_StatusId = ApprovedSettingType.Active;
                E.EStore_CategoryId = int.Parse(drdJob.SelectedValue);
                E.EStore_ProvinceId = int.Parse(drdCity.SelectedValue);
                E.Users_Id = int.Parse(drdUser.SelectedValue);
                dc.SubmitChanges();
                LoadInfo();
                pnlDetail.Visible = false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        
            try
            {
                using (Up1VNDataContext dc = new Up1VNDataContext())
                {
                    EStore E = new EStore();
                    E.EStore_Name = txtName.Text;
                    E.Users_Id = Int32.Parse(drdUser.SelectedValue);
                    E.EStore_CodeTax = txtTax.Text;
                    E.EStore_Representative = txtRepresent.Text;
                    E.Estore_HomePhone = txtPhone.Text;
                    E.EStore_MobilePhone = txtMobil.Text;
                    E.EStore_NickSkype = txtSky.Text;
                    E.EStore_NickYahoo = txtYahoo.Text;
                    E.EStore_Address = txtAddress.Text;
                    E.EStore_BriefIntroduction = txtDes.Text;
                    E.EStore_Website = txtWeb.Text;
                    E.EStore_Fax = txtFax.Text;
                    E.EStore_StatusId = ApprovedSettingType.InActive;
                    E.EStore_CategoryId = Int32.Parse(drdJob.SelectedValue);
                    E.EStore_ProvinceId = Int32.Parse(drdCity.SelectedValue);
                    E.EStore_Content = txtName.Text + "," + txtAddress.Text;
                    dc.EStores.InsertOnSubmit(E);
                    dc.SubmitChanges();
                    LoadInfo();
                    pnlDetail.Visible = false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
       
        
    }

    
    protected void imgAdd_Click(object sender, ImageClickEventArgs e)
    {
        foreach (Control c in pnlDetail.Controls)
        {
            if (c is TextBox)
            {
                TextBox txt = (TextBox)c;
                txt.Text = "";
            }
            if (c is Label)
            {
                Label lbl = (Label)c;
                lbl.Text = "";
            }
        }
        pnlDetail.Visible = true;
        btnSave.Visible = true;
        btnUpdate.Visible = false;
        try
        {
            using(Up1VNDataContext dc = new Up1VNDataContext())
            {
                var queryCateJob = (from o in dc.GeneralCategories 
                                    where o.GeneralCategory_Type == GeneralCategoryType.CategoryJob
                                select o);
                drdJob.DataSource = queryCateJob;
                drdJob.DataTextField = "GeneralCategory_Name";
                drdJob.DataValueField = "GeneralCategory_Id";
                drdJob.DataBind();

                var queryCateCity = (from o in dc.GeneralCategories
                                     where o.GeneralCategory_Type == GeneralCategoryType.CityProvince
                                     select o);
                drdCity.DataSource = queryCateCity;
                drdCity.DataTextField = "GeneralCategory_Name";
                drdCity.DataValueField = "GeneralCategory_Id";
                drdCity.DataBind();

                var queryUser = (from o in dc.Users select o);
                drdUser.DataSource = queryUser;
                drdUser.DataTextField = "Users_Name";
                drdUser.DataValueField = "Users_Id";
                drdUser.DataBind();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlDetail.Visible = false;
    }



   
   
}